package com.alex.algorithm.easy;

public class RemoveDuplicatesFromSortedArray {
    public static void main(String[] args) {
        int[] array = new int[]{1, 2, 2,3,4,4,5,6};
        int len = removeDuplicates(array, 8);
        System.out.println(len);
        System.out.println();
        for (int i = 0; i < len; ++i) {
            System.out.println(array[i]);
        }
    }

    public static int removeDuplicates(int[] arr, int n) {
        if (n == 0) return 0;

        int index = 0;
        for (int i = 1; i < n; ++i) {
            if (arr[index] != arr[i]) {
                index++;
                arr[index] = arr[i];
            }
        }
        return index + 1;
    }
}
